/**
 * Studentmanager.java
 * 
 * This class is used globally for creating and managing students.
 *
 * @author Felix Schuller
 */

package Runde2;

import java.util.HashMap;
import java.util.Map;

public class StudentManager {
	
	private static HashMap<String, Student> studMap = new HashMap<String, Student>();

	/**
	 * Creates a new student and adds it to the students-list.
	 * 
	 * @params see Person.
	 * @param matrNr - student's matriculation number
	 * @return created student's matriculation number, used to "get" the Student-object from StudentManager
	 */
	public static String createStud(String fName, String lName, String password, String matrNr) throws InvalidPersonException{
		try {
			if (Integer.parseInt(matrNr) <= 0 || String.valueOf(matrNr).length() != 7) {
				throw new InvalidPersonException("Invalid matrNr given: matrNr must be 7 digits long (given: " + String.valueOf(matrNr).length() + ")");
			}
		} catch (NumberFormatException e) {
			throw new InvalidPersonException("Invalid matrNr given: matrNr must be numeric");

		}

		try {
			studMap.put(matrNr, new Student(fName, lName, password));
		} catch (InvalidPersonException e) {
			throw new InvalidPersonException(e.getMessage());
		}
		return matrNr;
	}
	
	/**
	 * Returns Student with given matrNr from students-list.
	 * 
	 * @param matrNr - student's matriculation number
	 * @return Student with given matrNr
	 */
	public static Student getStud(String matrNr) {
		return studMap.get(matrNr);
	}
	
	/**
	 * Returns a readable form of students-list
	 * 
	 * @return readable form of students-list
	 */
	public static String getStudentList() {
		String output = "";
		for (Map.Entry<String, Student> s: studMap.entrySet()) {
			output += "#" + s.getKey() + ": " + s.getValue() + "\n";
		}
		return output;
		
	}
}
